import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Main from '../components/Main.vue'
import Users from '../components/users/Users.vue'
import ConstantType from '../components/constant/ConstantType.vue'
import ConstantItem from '../components/constant/ConstantItem.vue'
import Department from '../components/department/Department.vue'
import DiseCategory from '../components/disease/DiseCategory.vue'
import Disease from '../components/disease/Disease.vue'
import Rota from '../components/scheduling/Rota.vue'
import Rule from '../components/scheduling/Rule.vue'
import His_User from '../components/his_system/His_User.vue'
import RegisteLeve from '../components/registered/RegisteLeve.vue'
import SettleCategory from '../components/registered/SettleCategory.vue'


Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home,
        redirect: '/main',
        children: [
            { path: '/main', component: Main },
            { path: '/users', component: Users },
            { path: '/constanttype', component: ConstantType },
            { path: '/constantitem', component: ConstantItem },
            { path: '/his_user', component: His_User },
            { path: '/disecategory', component: DiseCategory },
            { path: '/disease', component: Disease },
            { path: '/rota', component: Rota },
            { path: '/rule', component: Rule },
            { path: '/registeLeve', component: RegisteLeve },
            { path: '/settlecategory', component: SettleCategory },
            { path: '/department', component: Department },
        ]

    }
]

const router = new VueRouter({
    routes
})

//如果用户退出登录，也就意味着用户不用访问首页(/home)了。
//通过挂载路由的导航守卫来实现这个功能
// to:就是我们要访问的路径
// from:表示从哪个路径跳转过来的
// next:它就是一个函数，表示放行
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
        //我们要获取到sessionStorage中的token
    const tokenS = window.sessionStorage.getItem('token')
        //如果token不存在，我们就要强制跳转
    if (!tokenS) return next('/login')
        //否则放行
    next()

})

export default router